package com.jdjw.modules.busassetarchives.controller;


import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busassetarchives.entity.BusAssetArchives;
import com.jdjw.modules.busassetarchives.entity.BusAssetArchivesSave;
import com.jdjw.modules.busassetarchives.entity.BusAssetArchivesVo;
import com.jdjw.modules.busassetarchives.service.BusAssetArchivesService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * bus_asset_archives 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-06-25
 */
@RestController
@RequestMapping("/busassetarchives")
public class BusAssetArchivesController {

    @Autowired
    private BusAssetArchivesService busAssetArchivesService;

    @ApiOperation("集体资产资源档案查询")
    @PostMapping("queryAll")
    public ResponseResult queryAll(BusAssetArchivesVo busAssetArchivesVo){
        //资产查询
        int start=busAssetArchivesVo.getPage()*busAssetArchivesVo.getRow()-busAssetArchivesVo.getRow();
        BusAssetArchives b=new BusAssetArchives();
        if(!StringUtils.isEmpty(busAssetArchivesVo.getType())){
            b.setType(busAssetArchivesVo.getType());
        }
        if(!StringUtils.isEmpty(busAssetArchivesVo.getStatus())){
            b.setStatus(busAssetArchivesVo.getStatus());
        }
        if(!StringUtils.isEmpty(busAssetArchivesVo.getIsPublic())){
            b.setIsPublic(busAssetArchivesVo.getIsPublic());
        }
        b.setStart(start);
        b.setRow(busAssetArchivesVo.getRow());
        return busAssetArchivesService.queryAll(b);
    }

    @ApiOperation("资产资源档案-添加")
    @PostMapping("save")
    public ResponseResult save(BusAssetArchivesSave busAssetArchivesSave){
        ResponseResult rr=new ResponseResult();
        try {
            rr.setCode(200);
            rr.setMsg("保存成功");
            BusAssetArchives baa=new BusAssetArchives();
            //转换
            BeanUtils.copyProperties(busAssetArchivesSave,baa);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            baa.setCreateDate(sdf.format(new Date()));//获取当前时间
            busAssetArchivesService.save(baa);
        } catch (BeansException e) {
            e.printStackTrace();
            rr.setCode(500);
            rr.setMsg("保存失败");
        }
        return rr;
    }

    @ApiOperation("资产资源档案-编辑")
    @PostMapping("edit")
    public ResponseResult edit(@RequestBody String sqlEdit) throws JSONException {
        //转换JSON格式
        JSONObject json=new JSONObject(sqlEdit);
        System.out.println("sql::::"+sqlEdit);
        return busAssetArchivesService.sqlEnit(json.getString("sqlEdit"));
    }


    @ApiOperation("资产资源档案-删除")
    @PostMapping("delete")
    public ResponseResult delete(String id){
        boolean b = busAssetArchivesService.removeById(id);
        ResponseResult rrt=new ResponseResult();
        if(b){
            rrt.setMsg("删除成功");
            rrt.setCode(200);
        }else{
            rrt.setMsg("删除失败");
            rrt.setCode(500);
        }
        return rrt;
    }

    @ResponseBody
    @ApiOperation("资产资源档案-查看")
    @PostMapping("{id}/preview")
    public ResponseResult preview(@ApiParam(value = "资产资源id",required = true) @PathVariable("id") String id){
        BusAssetArchives bus = busAssetArchivesService.getById(id);
        ResponseResult rrt=new ResponseResult();
        if(!StringUtils.isEmpty(bus)){
            rrt.setCode(200);
            rrt.setMsg("查看成功");
            rrt.setData(bus);
        }else{
            rrt.setCode(200);
            rrt.setMsg("查看失败");
            rrt.setData(null);
        }
        return rrt;
    }




}

